package com.woniuxy.conmon;


import com.woniuxy.conmon.enums.ResultCode;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常的处理
 */
@RestControllerAdvice
public class GlobalException {

    /**
     * 处理自定义业务异常
     */
    @ExceptionHandler(HealthBusinessException.class)
    public ResponseEntity<String> handlerBusinessException(HealthBusinessException e) {
        //调用get方法获取异常的错误码和错误信息
        ResultCode resultCode = e.getResultCode();
        //返回错误信息
        return ResponseEntity.status(resultCode.getCode()).body(resultCode.getMessage());
    }


    /**
     * 当发生未知异常时处理方法
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handlerException(Exception e) {
        //输出异常信息，方便调试
        e.printStackTrace();
        //返回错误信息
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
    }

}
